package util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import pojo.Book;

public class FileProcessDataUtil {
	public static ArrayList<Book> writeAndReadData(){
		Book book = null;
		ArrayList<Book> books = null;
		try {
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("books.txt"));
			for(int i=0;i<10000;i++){
				book = new Book(null, "Evan"+i, new Date(), "bilibili", 88.88, true, "珠海出版社");
				oos.writeObject(book);
			}
			oos.writeObject(null);
			oos.close();
			books = readData();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return books;
	}
	private static ArrayList<Book> readData(){
		ArrayList<Book> books = new ArrayList<Book>();
		try{
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream("books.txt"));
			Book book = null;
			while((book = (Book) ois.readObject()) != null){
				books.add(book);
			}
			ois.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return books;
	}
			
	public static void main(String[] args) {
		ArrayList<Book> writeAndReadData = writeAndReadData();
		System.out.println(writeAndReadData.size());
	}
	

}
